/*
 * @Author: xiaosihan 
 * @Date: 2024-08-04 22:13:07 
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2024-09-22 20:19:15
 */


import { Table } from "antd";
import { autorun } from "mobx";
import { useEffect, useState } from "react";
import styles from "./projectAnnotation.module.less";
import projectAnnotationStore from "./projectAnnotationStore";
import { render } from "react-dom";
import partLableApi from "@api/partCollaborateApi";
import { PartLableData } from "@type";
import { get } from "lodash";

interface Iprops {
    modelId?: string
}

// 项目下的零件批注
export default function ProjectAnnotationItem(props: Iprops) {

    const [list, setList] = useState(projectAnnotationStore.list);
    const [page, setPage] = useState(projectAnnotationStore.page);
    const [totalCount, setTotalCount] = useState(projectAnnotationStore.totalCount);

    useEffect(() => {
        (async () => {
            if (props.modelId) {
                const result = await partLableApi.get(1, 9999, props.modelId);
                const partLableDatas = get(result, ["data", "data"]) as Array<PartLableData>;
                setList(partLableDatas);
            }
        })();
    }, [props]);

    useEffect(() => autorun(() => {
        setList(projectAnnotationStore.list);
        setPage(projectAnnotationStore.page);
        setTotalCount(projectAnnotationStore.totalCount);
    }), []);

    function formatTimeDifference(timeString: string): string {
        const inputDate = new Date(timeString);
        const now = new Date();

        // 计算时间差（毫秒）  
        const diff = now.getTime() - inputDate.getTime();

        // 定义时间阈值和对应的输出  
        if (diff < 1000 * 60 * 5) { // 小于5分钟  
            return '刚刚';
        } else if (diff < 1000 * 60 * 60) { // 小于1小时  
            const minutes = Math.floor(diff / (1000 * 60));
            return `${minutes}分钟前`;
        } else if (diff < 1000 * 60 * 60 * 24) { // 小于24小时  
            const hours = Math.floor(diff / (1000 * 60 * 60));
            return `${hours}小时前`;
        } else if (
            diff >= 1000 * 60 * 60 * 24 &&
            diff < 1000 * 60 * 60 * 24 * 2
        ) { // 小于2天  
            // 检查是否是昨天（只考虑日期部分）  
            const yesterday = new Date(now);
            yesterday.setDate(yesterday.getDate() - 1);
            yesterday.setHours(0, 0, 0, 0); // 重置时间为00:00:00  
            if (
                inputDate.getUTCFullYear() === yesterday.getUTCFullYear() &&
                inputDate.getUTCMonth() === yesterday.getUTCMonth() &&
                inputDate.getUTCDate() === yesterday.getUTCDate()
            ) {
                return '昨天';
            }
            // 如果不满足“昨天”的条件，但由于我们已经检查了小于2天，这里实际上不会执行  
            // 但为了代码的完整性，我们仍然可以保留返回小时前的逻辑（尽管它不会被执行）  
            // 或者，你也可以选择返回一个更通用的“1天前”的消息  
            const hours = Math.floor(diff / (1000 * 60 * 60));
            return `${Math.ceil(hours / 24)}天前`; // 或者直接返回 '1天前'，但这样不够准确  
        }

        // 如果时间差超过2天，则返回日期字符串  
        return inputDate.toLocaleDateString();
    }

    const dataSource: any[] = [];
    list.map(l => {
        dataSource.push({
            key: l.id,
            id: l.id,
            content: `${dataSource.length + 1} ${l.content}`,
            user: l.userName,
            data: formatTimeDifference(l.createTime)
        });
    });

    const columns = [
        {
            title: '',
            align: "left",
            dataIndex: 'content',
            key: 'content'
        },
        {
            title: '',
            align: "center",
            dataIndex: 'user',
            key: 'user',
        },
        {
            title: '',
            align: "center",
            dataIndex: 'data',
            key: 'data',
            render(data: string) {
                return (
                    <span className={styles.data} >{data}</span>
                );
            }
        }
    ]

    return (
        <div className={styles.projectAnnotation} >
            <Table
                size="small"
                dataSource={dataSource}
                columns={columns as any}
                className={styles.table}
                pagination={false}
            // pagination={{
            //     current: page,
            //     total: totalCount,
            //     pageSize: 10,
            //     onChange: (page: number, pageSize: number) => {
            //         projectAnnotationStore.setPage(page);
            //     }
            // }}
            />
        </div>
    );

}